#! /bin/sh

# Test kedr_capture_trace functionality(no block, no session modes).

# Just in case the tools like lsmod are not in their usual location.
export PATH=$PATH:/sbin:/bin:/usr/bin

kedr_module_name="@CORE_MODULE_NAME@"
kedr_module="@CORE_MODULE@"
kedr_trace_module_name="@TRACE_MODULE_NAME@"
kedr_trace_module="@TRACE_MODULE@"
payload_module_name=kedr_payload
payload_module="@CMAKE_CURRENT_BINARY_DIR@/payload/${payload_module_name}.ko"
target_module_name=kedr_target
target_module="@CMAKE_CURRENT_BINARY_DIR@/target/${target_module_name}.ko"


tmpdir="@KEDR_TEST_PREFIX_TEMP_SESSION@/capture_trace"

trace_file1="${tmpdir}/trace1"
trace_file2="${tmpdir}/trace2"
mkdir -p "${tmpdir}"

debugfs_mount_point="${tmpdir}/debugfs"
mkdir -p "${debugfs_mount_point}"

capture_trace_program="@CAPTURE_TRACE_PROGRAM@"

# Load KEDR core and payload modules
insmod "${kedr_module}" "target_name=${target_module_name}"
if test $? -ne 0; then
	printf "Failed to load KEDR core module for tests\n"
	exit 1
fi

insmod "${kedr_trace_module}"
if test $? -ne 0; then
	printf "Failed to load kedr tracing module for tests\n"
    rmmod "${kedr_module_name}"
	exit 1
fi


insmod "${payload_module}"
if test $? -ne 0; then
	printf "Failed to load payload module for tests\n"
    rmmod "${kedr_trace_module_name}"
    rmmod "${kedr_module_name}"
	exit 1
fi

# Load target module
insmod "${target_module}"

if test $? -ne 0; then
    printf "Failed to load target module\n"
    kill $capture_pid
    wait $capture_pid
    umount "${debugfs_mount_point}"
    rmmod "${payload_module_name}"
    rmmod "${kedr_trace_module_name}"
    rmmod "${kedr_module_name}"
    exit 1
fi

mkdir -p "${debugfs_mount_point}"
mount debugfs -t debugfs "${debugfs_mount_point}"
if test $? -ne 0; then
    printf "Cannot mount debugfs.\n"
    rmmod "${target_module}"
    rmmod "${payload_module_name}"
    rmmod "${kedr_trace_module_name}"
    rmmod "${kedr_module_name}"
    exit 1
fi

# Capture trace for module loading
rm -f "${trace_file1}"
${capture_trace_program} -d "${debugfs_mount_point}" -f "${trace_file1}"

# Unload target module
rmmod "${target_module}"

# Capture trace for module unloading
rm -f "${trace_file2}"
${capture_trace_program} -d "${debugfs_mount_point}" -f "${trace_file2}"


umount "${debugfs_mount_point}"

#Unload payload and KEDR core and other modules
rmmod "${payload_module}"
rmmod "${kedr_trace_module_name}"
rmmod "${kedr_module}"

#Verify existance of both trace files
for trace_file in "${trace_file1}" "${trace_file2}"; do
    if ! test -f "${trace_file}"; then
        printf "Trace file %s wasn't created, but it should\n" "${trace_file}"
        exit 1
    fi
done
#Verify that both trace files contain some strings output by the payload
for trace_file in "${trace_file1}" "${trace_file2}"; do
    if ! grep "Capture me payload 2" "${trace_file}" > /dev/null; then
        printf "Module output a line to the trace but that line was not captured in file %s\n" "${trace_file}"
        exit 1
    fi
done
# Implementation specific!
#Verify, that first trace contain message about load of the target
if ! grep "target_session_begins" "${trace_file1}" > /dev/null; then
    printf "Beginning mark wasn't captured in file %s\n" "${trace_file1}"
    exit 1
fi

#Verify, that trace contain message about unload of the target
if ! grep "target_session_ends" "${trace_file2}" > /dev/null; then
    printf "Ending mark wasn't captured in file %s\n" "${trace_file2}"
    exit 1
fi

exit 0
